home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / ddj_cspc.zip / LADD.LST < prev    next >
File List  |  1989-11-21  |  12KB  |  496 lines

  1. _C++ STRING CLASSES_
  2. by Scott Robert Ladd
  3.  
  4. [LISTING ONE]
  5.  
  6.  
  7. //  Header:     String  (Dynamic Strings)
  8. //  Version:    1.01    13-Sep-1989
  9. //  Language:   C++ 2.0
  10. //  Environ:    Any
  11. //  Compilers:  Zortech C++
  12. //  Purpose:    Provides a general dynamic string class.
  13. //  Written by: Scott Robert Ladd
  14. //              705 West Virginia
  15. //              Gunnison CO 81230
  16. //              MCI ID:  srl
  17. //              FidoNet: 1:104/45.2
  18.  
  19. #if !defined(STRING_HPP)
  20. #define STRING_HPP
  21.  
  22. #include "stddef.h"
  23.  
  24. class String
  25.     {
  26.     private:
  27.         // instance variables
  28.         unsigned int Siz;    // allocated size
  29.         unsigned int Len;    // current length
  30.         char * Txt;          // pointer to text
  31.         // class constant
  32.         static unsigned int AllocIncr;
  33.         // private method used to shrink a string to its minimum allocation
  34.         void Shrink();
  35.     public:
  36.         enum StrCompVal  {SC_LESS, SC_EQUAL, SC_GREATER};
  37.         enum StrCompMode {SF_SENSITIVE, SF_IGNORE};
  38.         // constructor
  39.         String();
  40.         String(String & Str);
  41.         String(char * Cstr);
  42.         String(char FillCh, unsigned int Count);
  43.         // destructor
  44.         ~String();
  45.         // value return methods
  46.         unsigned int Length();
  47.         unsigned int Size();
  48.         // Function to return a blank string
  49.         friend String Empty();
  50.         // copy String to c-string method
  51.         void Copy(char * Cstr, unsigned int Max);
  52.         // create a c-string from String method
  53.         char * Dupe();
  54.         // assignment method
  55.         void operator = (String & Str);
  56.         // concatenation methods
  57.         friend String operator + (String Str1, String Str2);
  58.         void operator += (String Str);
  59.         // comparison method
  60.         StrCompVal Compare(String Str, StrCompMode Case = SF_IGNORE);
  61.         // substring search methods
  62.         int Find(String Str, unsigned int & Pos, StrCompMode Case = SF_IGNORE);
  63.         // substring deletion method
  64.         void Delete(unsigned int Pos, unsigned int Count);
  65.         // substring insertion methods
  66.         void Insert(unsigned int Pos, char Ch);
  67.         void Insert(unsigned int Pos, String Str);
  68.         // substring retrieval method
  69.         String SubStr(unsigned int Start, unsigned int Count);
  70.         // character retrieval method
  71.         char operator [] (unsigned int Pos);
  72.         // case-modification methods
  73.         String ToUpper();
  74.         String ToLower();
  75.     };
  76. #endif
  77.  
  78.  
  79. [LISTING TWO]
  80.  
  81. //  Module:     String  (Dynamic Strings)
  82. //  Version:    1.01    13-Sep-1989
  83. //  Language:   C++ 2.0
  84. //  Environ:    Any
  85. //  Compilers:  Zortech C++
  86. //  Purpose:    Provides a general dynamic string class.
  87. //  Written by: Scott Robert Ladd
  88. //              705 West Virginia
  89. //              Gunnison CO 81230
  90. //              MCI ID:  srl
  91. //              FidoNet: 1:104/45.2
  92.  
  93. #include "String.hpp"
  94. #include "string.h"
  95. #include "stddef.h"
  96. #include "ctype.h"
  97.  
  98. // class-global constant intialization
  99. unsigned int String::AllocIncr = 8;
  100.  
  101. // private function to shrink the size of an allocated string
  102. void String::Shrink()
  103.     {
  104.     char * Temp;
  105.     if ((Siz - Len) > AllocIncr)
  106.         {
  107.         Siz  = ((Len + AllocIncr - 1) / AllocIncr) * AllocIncr;
  108.         Temp = new char[Siz];
  109.         memcpy(Temp,Txt,Len);
  110.         delete Txt;
  111.         Txt  = Temp;
  112.         }
  113.     }
  114.  
  115. // constructor
  116. String::String()
  117.     {
  118.     Len    = 0;
  119.     Siz    = AllocIncr;
  120.     Txt    = new char[Siz];
  121.     Txt[0] = '\x00';
  122.     }
  123. String::String(String & Str)
  124.     {
  125.     Len = Str.Len;
  126.     Siz = Str.Siz;
  127.     Txt = new char[Siz];
  128.     memcpy(Txt,Str.Txt,Len);
  129.     }
  130. String::String(char * Cstr)
  131.     {
  132.     Len = strlen(Cstr);
  133.     Siz  = ((Len + AllocIncr - 1) / AllocIncr) * AllocIncr;
  134.     Txt = new char[Siz];
  135.     memcpy(Txt,Cstr,Len);
  136.     }
  137. String::String(char FillCh, unsigned int Count)
  138.     {
  139.     unsigned int Pos;
  140.     Siz = ((Count + AllocIncr - 1) / AllocIncr) * AllocIncr;
  141.     Len = Siz;
  142.     Txt = new char[Siz];
  143.     memset(Txt,FillCh,Count);
  144.     }
  145.  
  146. // destructor
  147. String::~String()
  148.     {
  149.     delete Txt;
  150.     }
  151.  
  152. // value return methods
  153. unsigned int String::Length()
  154.     {
  155.     return Len;
  156.     }
  157. unsigned int String::Size()
  158.     {
  159.     return Siz;
  160.     }
  161.  
  162. // Function to return a blank string
  163. String Empty()
  164.     {
  165.     static String EmptyStr;
  166.     return EmptyStr;
  167.     }
  168.  
  169. // copy String to c-string method
  170. void String::Copy(char * Cstr, unsigned int Max)
  171.     {
  172.     unsigned int CopyLen;
  173.     if (Max == 0)
  174.         return;
  175.     if (Len >= Max)
  176.         CopyLen = Max - 1;
  177.     else
  178.         CopyLen = Len;
  179.     memcpy(Cstr,Txt,CopyLen);
  180.     Cstr[CopyLen] = '\x00';
  181.     }
  182.  
  183. // create a c-string from String method
  184. char * String::Dupe()
  185.     {
  186.     char * new_cstr;
  187.     new_cstr = new char[Len + 1];
  188.     memcpy(new_cstr,Txt,Len);
  189.     new_cstr[Len] = '\x00';
  190.     return new_cstr;
  191.     }
  192.  
  193. // assignment method
  194. void String::operator = (String & Str)
  195.     {
  196.     Len    = Str.Len;
  197.     Siz    = Str.Siz;
  198.     delete Txt;
  199.     Txt = new char[Siz];
  200.     memcpy(Txt,Str.Txt,Len);
  201.     }
  202.  
  203. // concatenation methods
  204. String operator + (String Str1, String Str2)
  205.     {
  206.     unsigned int NewLen, NewSiz, CopyLen;
  207.     String TempStr;
  208.     char * Temp;
  209.     TempStr = Str1;
  210.     CopyLen = Str2.Len;
  211.     NewLen  = TempStr.Len + Str2.Len;
  212.     NewSiz  = TempStr.Siz + Str2.Siz;
  213.     Temp = new char[NewSiz];
  214.     memcpy(Temp,TempStr.Txt,TempStr.Len);
  215.     delete TempStr.Txt;
  216.     TempStr.Txt = Temp;
  217.     memcpy(&TempStr.Txt[TempStr.Len],Str2.Txt,CopyLen);
  218.     TempStr.Len = NewLen;
  219.     TempStr.Siz = NewSiz;
  220.     TempStr.Shrink();
  221.     return TempStr;
  222.     }
  223. void String::operator += (String Str)
  224.     {
  225.     unsigned int NewLen, NewSiz, CopyLen;
  226.     char * Temp;
  227.     CopyLen = Str.Len;
  228.     NewLen  = Len + CopyLen;
  229.     NewSiz  = Siz + Str.Siz;
  230.     Temp = new char[NewSiz];
  231.     memcpy(Temp,Txt,Len);
  232.     delete Txt;
  233.     Txt = Temp;
  234.     memcpy(&Txt[Len],Str.Txt,CopyLen);
  235.     Len = NewLen;
  236.     Siz = NewSiz;
  237.     Shrink();
  238.     }
  239.  
  240. // comparison method
  241. StrCompVal String::Compare(String Str, StrCompMode Case)
  242.     {
  243.     char * Temp1, * Temp2;
  244.     Temp1 = new char[Len + 1];
  245.     Copy(Temp1,Len+1);
  246.     Temp2 = new char[Str.Len + 1];
  247.     Str.Copy(Temp2,Str.Len+1);
  248.     if (Case == SF_IGNORE)
  249.         {
  250.         strupr(Temp1);
  251.         strupr(Temp2);
  252.         }
  253.     switch (strcmp(Temp1,Temp2))
  254.         {
  255.         case -1: return SC_LESS;
  256.         case  0: return SC_EQUAL;
  257.         case  1: return SC_GREATER;
  258.         }
  259.     delete Temp1;
  260.     delete Temp2;
  261.     }
  262.  
  263. // substring search methods
  264. int String::Find(String Str, unsigned int & Pos, StrCompMode Case)
  265.     {
  266.     char * TempStr1, * TempStr2;
  267.     unsigned int LastPos, SearchLen, TempPos;
  268.     int Found;
  269.     TempStr1 = new char[Len + 1];
  270.     memcpy(TempStr1,Txt,Len);
  271.     TempStr1[Len] = '\x00';
  272.     TempStr2 = new char[Str.Len + 1];
  273.     memcpy(TempStr2,Str.Txt,Str.Len);
  274.     TempStr2[Str.Len] = '\x00';
  275.     if (Case == SF_IGNORE)
  276.         {
  277.         strupr(TempStr1);
  278.         strupr(TempStr2);
  279.         }
  280.     Pos     = 0;
  281.     TempPos = 0;
  282.     Found   = 0;
  283.     SearchLen = Str.Len;
  284.     LastPos   = Len - SearchLen;
  285.     while ((TempPos <= LastPos) && !Found)
  286.         {
  287.         if (0 == strncmp(&TempStr1[TempPos],TempStr2,SearchLen))
  288.             {
  289.             Pos   = TempPos;
  290.             Found = 1;
  291.             }
  292.         else
  293.             ++TempPos;
  294.         }
  295.     delete TempStr1;
  296.     delete TempStr2;
  297.     return Found;
  298.     }
  299.  
  300. // substring deletion method
  301. void String::Delete(unsigned int Pos, unsigned int Count)
  302.     {
  303.     unsigned int CopyPos;
  304.     if (Pos > Len)
  305.         return;
  306.     CopyPos = Pos + Count;
  307.     if (CopyPos >= Len)
  308.         Txt[Pos] = 0;
  309.     else
  310.         while (CopyPos <= Len)
  311.             {
  312.             Txt[Pos] = Txt[CopyPos];
  313.             ++Pos;
  314.             ++CopyPos;
  315.             }
  316.     Len -= Count;
  317.     Shrink();
  318.     }
  319.  
  320. // substring insertion methods
  321. void String::Insert(unsigned int Pos, char Ch)
  322.     {
  323.     char * Temp;
  324.     if (Pos > Len)
  325.         return;
  326.     if (Len == Siz)
  327.         {
  328.         Siz += AllocIncr;
  329.         Temp = new char[Siz];
  330.         memcpy(Temp,Txt,Len);
  331.         delete Txt;
  332.         Txt = Temp;
  333.         }
  334.     if (Pos < Len)
  335.         for (unsigned int Col = Len + 1; Col > Pos; --Col)
  336.             Txt[Col] = Txt[Col-1];
  337.     Txt[Pos] = Ch;
  338.     ++Len;
  339.     }
  340. void String::Insert(unsigned int Pos, String Str)
  341.     {
  342.     unsigned int SLen = Str.Len;
  343.     SLen = Str.Len;
  344.     if (SLen > 0)
  345.         for (unsigned int I = 0; I < SLen; ++I)
  346.             {
  347.             Insert(Pos,Str.Txt[I]);
  348.             ++Pos;
  349.             }
  350.     }
  351.  
  352. // substring retrieval method
  353. String String::SubStr(unsigned int Start, unsigned int Count)
  354.     {
  355.     String TempStr;
  356.     char * Temp;
  357.     if ((Start < Len) && (Count > 0))
  358.         for (unsigned int Pos = 0; Pos < Count; ++Pos)
  359.             {
  360.             if (TempStr.Len == TempStr.Siz)
  361.                 {
  362.                 TempStr.Siz += AllocIncr;
  363.                 Temp = new char[TempStr.Siz];
  364.                 memcpy(Temp,TempStr.Txt,Len);
  365.                 delete TempStr.Txt;
  366.                 TempStr.Txt = Temp;
  367.                 }
  368.             TempStr.Txt[Pos] = Txt[Start + Pos];
  369.             ++TempStr.Len;
  370.             }
  371.     return TempStr;
  372.     }
  373.  
  374. // character retrieval method
  375. char String::operator [] (unsigned int Pos)
  376.     {
  377.     if (Pos >= Len)
  378.         return '\x00';
  379.     return Txt[Pos];
  380.     }
  381.  
  382. // case-modification methods
  383. String String::ToUpper()
  384.     {
  385.     String TempStr = *this;
  386.     for (unsigned int Pos = 0; Pos < Len; ++Pos)
  387.         TempStr.Txt[Pos] = toupper(TempStr.Txt[Pos]);
  388.     return TempStr;
  389.     }
  390. String String::ToLower()
  391.     {
  392.     String TempStr = *this;
  393.     for (unsigned int Pos = 0; Pos < Len; ++Pos)
  394.         TempStr.Txt[Pos] = tolower(TempStr.Txt[Pos]);
  395.     return TempStr;
  396.     }
  397.  
  398.  
  399. [LISTING THREE]
  400.  
  401. #include "String.hpp"
  402. #include "stdio.h"
  403. #include "stream.hpp"
  404.  
  405. int main();
  406. void print_string(String S);
  407.  
  408. String s1;
  409. String s2("This is the second string!");
  410.  
  411. int main()
  412.     {
  413.     String ls;
  414.     String ls2("Another local string");
  415.     unsigned int pos, i;
  416.     char ch;
  417.  
  418.     s1 = s2;
  419.     ls = "This is the local string.";
  420.     print_string(s1);
  421.     print_string(s2);
  422.     print_string(ls);
  423.     print_string(ls2);
  424.     cout << "\n";
  425.  
  426.     s1 = s2 + ls;
  427.     print_string(s1);
  428.  
  429.     s1 = "String one has a value.";
  430.     print_string(s1);
  431.  
  432.     s1 = s1 + "****";
  433.     print_string(s1);
  434.  
  435.     s2 += "*****";
  436.     print_string(s2);
  437.     print_string(ls);
  438.  
  439.     s2 += ls;
  440.     print_string(s2);
  441.  
  442.     cout << "\n";
  443.  
  444.     if (s2.Find("Burfulgunk",pos))
  445.         printf("first search = %d\n",pos);
  446.     if (s2.Find("*****",pos))
  447.         printf("second search = %d\n",pos);
  448.     if (s2.Find(ls,pos))
  449.         printf("third search = %d\n",pos);
  450.     if (s2.Find(s1,pos))
  451.         printf("fourth search = %d\n",pos);
  452.     ls2 = "&&";
  453.  
  454.     s1.Insert(10,'*');
  455.     s1.Insert(15,ls2);
  456.     print_string(s1);
  457.  
  458.     s1.Insert(s1.Length(),'%');
  459.     s1.Insert(s1.Length(),'%');
  460.     s1.Insert(s1.Length(),'%');
  461.     s1.Insert(s1.Length(),'%');
  462.     print_string(s1);
  463.  
  464.     for (i = 0; 0 != (ch = s1[i]); ++i)
  465.         putchar(ch);
  466.     putchar('\n');
  467.  
  468.     s1.Insert(2,"<><><><><>");
  469.     print_string(s1);
  470.  
  471.     s1.Delete(2,10);
  472.     print_string(s1);
  473.  
  474.     s2 = s1.ToUpper();
  475.     print_string(s2);
  476.  
  477.     s2 = s1.ToLower();
  478.     print_string(s2);
  479.  
  480.     s1 = Empty();
  481.     print_string(s1);
  482.  
  483.     s1 = s2.SubStr(2,10);
  484.     print_string(s1);
  485.  
  486.     return 0;
  487.     }
  488. void print_string(String S)
  489.     {
  490.     char * cs;
  491.     cs = S.Dupe();
  492.     cout << cs << " Len = " << S.Length() << " Siz = " << S.Size() << "\n";
  493.     delete cs;
  494.     }
  495.  
  496.